package service.impl;

import domain.Video;
import domain.VideoCategory;
import exceptions.GeneralServiceException;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import service.IVideoService;

/**
 *
 * @author yuyok
 */
@Stateless
@LocalBean
public class VideoServiceImpl extends GenericServiceImpl<Video> implements IVideoService {

    @PersistenceContext(unitName = "onlinetvPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public VideoServiceImpl() {
        super(Video.class);
    }

    @Override
    public void validateVideo(String name) throws IllegalArgumentException {
        if (name == null) {
            throw new IllegalArgumentException("Name for validate cannot be null!");
        }
        
        Video video = getVideoByName(name);
        
        video.setIsEnable(true);
        
        try {
            save(video);
        } catch (GeneralServiceException ex) {
            Logger.getLogger(VideoServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public Video getVideoByName(String name) throws IllegalArgumentException {
        if (name == null) {
            throw new IllegalArgumentException("Category name cannot be null while getting by this field");
        }
        
        Query query = getEntityManager().createNamedQuery("getVideoByName");
        query.setParameter("name", name);
        
        try {
            return (Video) query.getSingleResult();
        } catch (NoResultException noRes) {
            return null;
        }
    }
    
//    @Override
//    protected void beforeEntityDelete(Video entity) {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }

}
